package transla;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.LinkedList;
public class Translar extends JFrame {

	private JPanel contentPane;
	private JTextArea input ;
	private JTextArea output;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Translar frame = new Translar();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Translar() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("find");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {		  
		    		String str;
		    		String str1="";
		    		String str2;
		    		str=input.getText();
		    		PreparedStatement presql;
		    		char ch[]=str.toCharArray();
					Class.forName("com.mysql.jdbc.Driver");
					System.out.printf("加载数据库");       	   
					Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8","root","");		
				    System.out.printf("\n连接数据库成功");				   
				    ResultSet rs;
				    if(ch[0]>'A'&&ch[0]<'z') 
				    {
				      presql=con.prepareStatement("select * from translate where english=?");
				      presql.setString(1,str);
				      rs=presql.executeQuery();				     
				      while(rs.next()) {
				    	  str2=rs.getString(2);
				    	  str1=str1+","+str2;			    	
				       }
				      output.setText(str1);
				    }
				    else {
				    	presql=con.prepareStatement("select * from translate where chinese=?");
					      presql.setString(1,str);
					      rs=presql.executeQuery();					 
					      while(rs.next()) {
					    	  str2=rs.getString(1);
					    	  str1=str1+","+str2;
				    	
					      }
					      output.setText(str1);
				    }
		    	  } catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				}
		    	  catch (SQLException e1) {
					e1.printStackTrace();
					}   	 
			}
		});
		btnNewButton.setBounds(260, 80,135, 66);
		contentPane.add(btnNewButton);
		
		input = new JTextArea();
		input.setBounds(10, 10, 131, 86);
		contentPane.add(input);
		
	    output = new JTextArea();
		output.setBounds(10, 127, 131, 110);
		contentPane.add(output);
	}
}